<?php

// @formatter:off
/**
 * @file collection.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: collection.php 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/frontview/actions/content/collection.php $
 */
// @formatter:on

class ActionControllerContentCollection extends WCActionController {
	//
	// Public methods.
	public function check() {
		$check = parent::check();

		if($check == WC_HTTPERROR_OK) {
			global $wcProfile;
			global $wcCollection;

			if(!$wcProfile->allowedTo(WC_PERM_COLLECTION_VIEW)) {
				global $errno;
				global $error;

				$check = WC_HTTPERROR_FORBIDDEN;
				$errno = $check;
				$error = "You're not allowed to list collections";
			}
			if($check == WC_HTTPERROR_OK && (!$wcCollection || !$wcCollection->ok())) {
				global $errno;
				global $error;

				$check = WC_HTTPERROR_FORBIDDEN;
				$errno = $check;
				$error = "Invalid collection";
			}
		}

		return $check;
	}
	//
	// Protected methods.
	protected function subRun() {
		$ok = true;

		//
		// Loading global pointers.
		global $wcSkinConfig;
		global $wcCollection;
		global $wcProfile;

		$this->_manager->PATH = "{$wcCollection->name()}";
		$this->_manager->title = "Collection '{$wcCollection->name()}'";
		// 
		// Loading comics list.
		// @{
		$showComics = false;
		$comics = array();
		$items = $wcCollection->comics();
		uasort($items, "WCComic::NameSort");
		foreach($items as $comic) {
			$showComics = true;
			$comics[] = WCFVBoxes::GetComicPreviewAssigns($comic);
		}
		$this->_manager->comics = $comics;
		if($showComics) {
			$this->_manager->show_comics = true;
		}
		// @}
		// 
		// Loading images list.
		// @{
		$showImages = false;
		$images = array();
		$items = $wcCollection->images();
		$flowUri = WCFlowManager::I()->urlFlowFor($wcCollection);
		uasort($items, "WCImage::NameSort");
		foreach($items as $image) {
			$showImages = true;
			$images[] = WCFVBoxes::GetImagePreviewAssigns($image, false, $flowUri);
		}
		$this->_manager->images = $images;
		if($showImages) {
			$this->_manager->show_images = true;
		}
		// @}
		// 
		// Loading collections list.
		// @{
		$showCollections = false;
		$collections = array();
		$items = $wcCollection->collections();
		uasort($items, "WCCollection::Sort");
		foreach($items as $collection) {
			$showCollections = true;
			$collections[] = WCFVBoxes::GetCollectionPreviewAssigns($collection);
		}
		$this->_manager->collections = $collections;
		if($showCollections) {
			$this->_manager->show_collections = true;
		}
		// @}
//		if($wcCollection->ok() && !$wcCollection->valid_preview) {
//			$wcCollection->generatePreviews();
//		}

		if($wcProfile->allowedTo(WC_PERM_COLLECTION_EDIT)) {
			$this->_manager->info_coll_editable = true;
		}
		if($wcProfile->allowedTo(WC_PERM_COLLECTION_RENAME)) {
			$this->_manager->info_name_editable = true;
		}
//		$this->_manager->info_count_comic = $comicsCount;
//		$this->_manager->info_count_image = $imagesCount;
//		$this->_manager->info_count_collections = $collectionsCount;
		if($wcCollection->parent) {
			$parent = WCCollectionsHolder::I()->item($wcCollection->parent);
			if($parent && $parent->ok()) {
				$this->_manager->info_collections = $parent->collectionPath();
			}
		}

		WCViewInfoFactory::LoadInfo($wcCollection, true);
		WCViewInfoFactory::LoadInterfacesInfo($wcCollection, true);

		return $ok;
	}
}
